package cn.maszy.common;

import java.util.Map;

import cn.maszy.action.systemsetting.LoginAction;
import cn.maszy.model.systemsetting.B_T_Ufp_Login;

import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.AbstractInterceptor;


public class LoginFilter extends AbstractInterceptor{
	private static final long serialVersionUID = 1L;
	// 用户信息session保存
	public static final String USER_SESSION = "UserSession";
	@Override
	public String intercept(ActionInvocation invocation) throws Exception {
		// TODO Auto-generated method stub
		// 取得Session
				Map session = invocation.getInvocationContext().getSession();
				// 从Session里获得登录时保存仅Session的User类
			B_T_Ufp_Login user = (B_T_Ufp_Login)ActionContext.getContext().getSession().get(USER_SESSION);
				 Action action = (Action)invocation.getAction();
		        // 登录画面不检测
				if (action instanceof  LoginAction) {   
			           return invocation.invoke();   
			    }  
		        // 验证码不检测
				if (action instanceof  Image) {   
			           return invocation.invoke();   
			    } 
				// 判断用户是否为空
				boolean isAuthenticated = (null!=user);
				if (!isAuthenticated) {// 如果未通过登录验证
					return Action.INPUT;
				} else {
					return invocation.invoke(); // 返回验证通过
				}
	}

	public void destroy() {}
	public void init() {}
}
